extends RefCounted
class_name ActionMgr

var undo_redo:UndoRedo = UndoRedo.new()

var last_acion:String

func print_action_list():
	print("_______________________________")
	for i in undo_redo.get_history_count():
		print(undo_redo.get_action_name(i))
	print("_______________________________")

func register_action(action:Action):
	undo_redo.create_action(action.action_name,action.merge_mode)
	undo_redo.commit_action(false)
	print_action_list()

func register_spawn(spawn_action:Spawn):
	var spawn_node = spawn_action.spawned_node
	var parent_node = spawn_action.parent
	var spawner_mgr = spawn_action.spawner
	
	undo_redo.create_action(spawn_action.action_name,spawn_action.merge_mode)
	undo_redo.add_do_method(spawner_mgr.request_certain_instance.bind(spawn_node))
	undo_redo.add_do_method(parent_node.add_child.bind(spawn_node))
	undo_redo.add_do_property(spawn_node,"position",spawn_node.position)
	undo_redo.add_undo_method(spawner_mgr.recycle_instance.bind(spawn_node))
	undo_redo.commit_action(false)
	print_action_list()

func register_node_die(die_action:Die,cemetery_mgr:CemeteryMgr):
	var dead_node = die_action.dead_node
	var parent = die_action.parent
	
	undo_redo.create_action(die_action.action_name,die_action.merge_mode)
	undo_redo.add_do_method(cemetery_mgr.save_node.bind(dead_node))
	undo_redo.add_undo_method(cemetery_mgr.get_node.bind(dead_node))
	undo_redo.add_undo_method(parent.add_child.bind(dead_node))
	undo_redo.commit_action(false)
	print_action_list()

func register_move(move_action:Move):
	var move_node:Node = move_action.moved_node
	var pre_position:Vector2 = move_action.pre_positioin
	var current_position:Vector2 = move_action.current_position
	undo_redo.create_action(move_action.action_name,move_action.merge_mode)
	undo_redo.add_do_property(move_node,"position",current_position)
	undo_redo.add_undo_property(move_node,"position",pre_position)
	undo_redo.commit_action(false)
	print_action_list()

func register_health_change(health_changed_action:HealthChange):
	var healthcomponent:HealthComponent = health_changed_action.health_component
	var num:int = health_changed_action.change_num
	
	undo_redo.create_action(health_changed_action.action_name,health_changed_action.merge_mode)
	undo_redo.add_do_method(healthcomponent.change_current_health.bind(num))
	undo_redo.add_undo_method(healthcomponent.change_current_health.bind(-num))
	undo_redo.commit_action(false)
	print_action_list()

class Action extends RefCounted:
	var action_name:String
	var merge_mode:UndoRedo.MergeMode = UndoRedo.MERGE_DISABLE
	func set_action_name(name:String):
		action_name = name
	func set_merge_mode(merge_mode_:UndoRedo.MergeMode):
		merge_mode = merge_mode_

class Spawn extends Action:
	var spawned_node:Node
	var parent:Node
	var spawner:Spawner
	
	func set_spawn_node_and_parent(node:Node):
		spawned_node = node
		var parent_ = spawned_node.get_parent()
		if parent_:
			parent = parent_
	func set_spawner(spawner_:Spawner):
		spawner = spawner_

class Die extends Action:
	var dead_node:Node2D
	var parent:Node
	
	func set_die_node_and_parent(node:Node):
		dead_node = node
		var parent_node_ = dead_node.get_parent()
		if parent_node_:
			parent = parent_node_

class Move extends Action:
	var moved_node:Node
	var pre_positioin:Vector2
	var current_position:Vector2
	
	func set_moved_node(node:Node):
		moved_node = node
	func set_move_position_changed(pre_positioin_:Vector2,current_position_:Vector2):
		pre_positioin = pre_positioin_
		current_position = current_position_

class HealthChange extends Action:
	var health_component:HealthComponent
	var change_num:int
	
	func set_changed_health_component(health_component_:HealthComponent):
		health_component = health_component_
	func set_changed_num(num:int):
		change_num = num
